//
//  YLSearchViewController.m
//  Style
//
//  Created by YL on 15/9/30.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "YLSearchViewController.h"
#import "AFNetWorkingW.h"
#import "YLSearchModel.h"
#import "YLSearchWebViewController.h"
#import "YLSearchDetailViewController.h"
@interface YLSearchViewController ()<UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UISearchBar *searchBar;
@property (nonatomic, assign) NSInteger start;
@property (nonatomic, retain) NSMutableArray *mArr;
@property (nonatomic, retain) UITableView *tableView;
@end

@implementation YLSearchViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    /* 添加serchBar */
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(15, 25, 40, 35);
    [button setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    self.title = @"搜索";
    self.start = 0;
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(50, 25, self.view.frame.size.width - 65, 35)];
    self.searchBar.placeholder = @"搜索文章";
    self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
    self.searchBar.tintColor = [UIColor blackColor];
    self.searchBar.delegate = self;
    [self.view addSubview:self.searchBar];
    [self tableViewSubView];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    self.mArr = [NSMutableArray array];
    NSString *url = [NSString stringWithFormat: @"auth=&client=1&deviceid=BA920378-52B5-43F2-BE1A-5404120EDD7A&keyword=%@&limit=10&start=%ld&type=content&version=3.0.6", [searchText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], (long)self.start];
    [AFNetWorkingW netWorkWithURL:@"http://api2.pianke.me/search/get" body:url resultBlock:^(id result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        NSArray *arr = [dic objectForKey:@"list"];
        for (NSDictionary *dic in arr) {
            YLSearchModel *searchModel = [[YLSearchModel alloc] init];
            [searchModel setValuesForKeysWithDictionary:dic];
            [searchModel setIcon:[[dic objectForKey:@"userinfo"] objectForKey:@"icon"]];
            [self.mArr addObject:searchModel];
        }
        [self.tableView reloadData];
    }];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    YLSearchDetailViewController *detail = [[YLSearchDetailViewController alloc] init];
    detail.searchText = searchBar.text;
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)tableViewSubView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 44) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    YLSearchModel *model = [self.mArr objectAtIndex:indexPath.row];
    cell.textLabel.text = model.title;
    
    NSString *string = cell.textLabel.text;
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
    
    UIColor *color = [UIColor redColor];
    [attrString addAttribute:NSForegroundColorAttributeName value:color range:[string rangeOfString:self.searchBar.text]];
    
    cell.textLabel.attributedText = attrString;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLSearchWebViewController *head = [[YLSearchWebViewController alloc] init];
    head.searchModel = [self.mArr objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:head animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
